package com.youlu.campus.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

@Slf4j
public class JwtUtils {

    private static final String JWT_SECRET = "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=";

    public static Claims verifyJwt(String token) {
        //签名秘钥，和生成的签名的秘钥一模一样
        try {
            SecretKey key = generalKey();
            Claims data = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
            return data;
        } catch (Exception e) {
            log.error("verify exception e",e);
        }
        return null;
    }

    /**
     * 由字符串生成加密key
     *
     * @return
     */
    public static SecretKey generalKey() {
        byte[] encodedKey = Base64.getDecoder().decode(JWT_SECRET);
        SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
        return key;
    }

}
